package com.todo.content.application.handler.changeStatus;

import com.alibaba.fastjson.JSONObject;
import com.todo.content.application.entity.bo.ContentDetailBO;
import com.todo.content.application.entity.context.ContentStatusContext;
import com.todo.content.application.service.ContentInfoService;
import com.todo.content.common.enums.ContentStatusEnum;
import com.todo.content.infrastructure.helper.es.entity.ContentDetailEntity;
import com.todo.content.infrastructure.helper.es.mapper.ContentDetailEntityMapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/12/12 19:37
 * @description
 */
@Slf4j
@Component
public class DisableHandler extends AbstractContentStatusHandler {

    @Resource
    private ContentInfoService contentInfoService;
    @Resource
    private ContentDetailEntityMapper contentDetailEntityMapper;


    @Override
    protected Boolean doStatusHandle(ContentStatusContext context) {
        Long contentId = context.getContentId();
        ContentDetailBO contentDetailBO = contentInfoService.queryContentDetailById(contentId);
        if (contentDetailBO == null) {
            log.error("DisableHandler#doStatusHandle 发布状态处理完成，数据为空，默认执行成功 context:{}", JSONObject.toJSONString(context));
            return true;
        }
        if (!contentDetailBO.getContentInfoBO().getStatus().equals(context.getCurrentStatus())) {
            log.error("DisableHandler#doStatusHandle 当前数据状态与发送消息状态不一致，无法处理当前状态下的数据情况 context:{}，当前状态 :{}", JSONObject.toJSONString(context), contentDetailBO.getContentInfoBO().getStatus());
            return true;
        }
        LambdaEsQueryWrapper<ContentDetailEntity> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(ContentDetailEntity::getMysqlId, contentId);
        contentDetailEntityMapper.delete(wrapper);
        return true;
    }

    @Override
    protected boolean match(ContentStatusContext context) {
        if (context == null || context.getContentId() == null || context.getCurrentStatus() == null) {
            log.error("DisableHandler#match 前置数据匹配失败，数据缺失或不存在 context:{}", JSONObject.toJSONString(context));
            return false;
        }
        return true;
    }

    @Override
    protected ContentStatusEnum doGetDealStatus() {
        return ContentStatusEnum.DISABLE;
    }
}
